package com.ips.service.impl;


import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import com.ips.service.ConversionService;

import com.ips.vo.ConversionLinkIVO;
import com.ips.vo.ConversionLinkOVO;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ConversionServiceImpl implements ConversionService {

    @Value("${conversion.url}")
    private String conversionUrl;

    @Override
    public ConversionLinkOVO getLink(ConversionLinkIVO ivo) {
        ConversionLinkOVO conversionLinkOVO = new ConversionLinkOVO();

        String url = ivo.getUrl();
        // 处理多条链接
        List<String> urlSplit = StrUtil.split(url, "\n", true, true);
        // 按照'|'来分割多个链接
        String urlJoin = StrUtil.join("|", urlSplit);
        // encode转换
        String urlEncode = URLUtil.encodeAll(urlJoin);

        // 指想要生成的配置类型
        String target = ivo.getTarget();
        String targetEncode = URLUtil.encodeAll(target);
        conversionLinkOVO.setUrl(conversionUrl + "target=" + targetEncode + "&url=" + urlEncode);
        return conversionLinkOVO;
    }
}
